#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

// 定义账户和初始余额
float balance = 1000.0;
char account[120] = "张三123";
int password = 123456;


//退出函数
void exit_(){
    char input;
    printf("\n\n\n按回车键返回上级目录" );
    while (getchar() != '\n');
    while (1) {
        input = getchar();
        if (input == '\n') {
            printf("正在退出中...\n");
            sleep(1);
            break;
        }
    }
}

//登陆函数
void start_(){
    char cmp1[120];
    int  cmp2;
    system("clear");
    printf("欢迎进入ATM小程序\n");
    printf("请输入您的银行账号:");
    scanf("%s",cmp1);
    printf("\n请输入您的银行账号密码:");
    scanf("%d",&cmp2);
    //?0:start_();
    if((!strcmp(account,cmp1) && password==cmp2)){

        printf("\n输入正确,正在进入菜单...");
        fflush(stdout);
        sleep(1);
    }else{
        printf("\n输入错误,请重新登陆");
        exit_();
        start_();
    }

}


// 函数：显示菜单
void show_menu() {
    printf("\n\t=== ATM 菜单 ===\n");
    printf("1. 查询余额\n");
    printf("2. 存款\n");
    printf("3. 取款\n");
    printf("4. 退出\n");
    printf("请输入选项: ");
}

// 函数：查询余额
void check_balance() {
    printf("当前余额: %.2f 元\n", balance);
    exit_();
}

// 函数：存款
void deposit() {
    float amount;
    printf("请输入存款金额: ");
    scanf("%f", &amount);

    if (amount > 0) {
        balance += amount;
        printf("存款成功！当前余额: %.2f 元\n", balance);
    } else {
        printf("存款金额无效！\n");
    }
    exit_();
}

// 函数：取款
void withdraw() {
    float amount;
    printf("请输入取款金额: ");
    scanf("%f", &amount);

    if (amount > 0 && amount <= balance) {
        balance -= amount;
        printf("取款成功！当前余额: %.2f 元\n", balance);
    } else if (amount > balance) {
        printf("余额不足！\n");
    } else {
        printf("取款金额无效！\n");
    }
    exit_();
}

int main() {
    int choice;
    start_();

    while (1) {
        system("clear");
        show_menu(); // 显示菜单
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                check_balance(); // 查询余额
                break;
            case 2:
                deposit(); // 存款
                break;
            case 3:
                withdraw(); // 取款
                break;
            case 4:
                printf("感谢使用，再见！\n");
                exit(0); // 退出程序
            default:
                printf("无效选项，请重新选择！\n");
        }
    }
    sleep(1);

    return 0;
}